

$(document).ready(function(){

$("#edit_mode,#loading").hide();

$("a#add_new_link").click(function(){
$("#sampleform")[0].reset();
show_NF();
$("#hp_id").val("");

$("#edit_mode").slideDown("slow");
$("#submit_form").val("Add SP");
});
});


function hp_detail_val(){
var myform = new Array();
var hp_id;
var data = $("#sampleform").serializeArray();
var a = "{";

if($("#habitation_name").val()==""){
alert('Enter Habitation Name');
$("#habitation_name").focus();
return false;
}

if($("#function_on").attr("checked")){
			//alert($("#fluctuation_min").val()+ "---" + $("#fluctuation_max").val() );
			
			if($("#fluctuation_min").val()!="" &&  $("#fluctuation_max").val()!="") {
				if($("#fluctuation_min").val() > $("#fluctuation_max").val()){
				alert("Water Table Fluctuations (m) Min must less than  Water Table Fluctuations (m) Max");
				$("#fluctuation_max").focus();
				return false;
				}
			}
			
	}

/*
if($("#location").val()==""){
alert('Enter Location');
$("#location").focus();
return false;
}
if($("#latitude").val()==""){
alert('Enter Latitude');
$("#latitude").focus();
return false;
}
if($("#longitude").val()==""){
alert('Enter Longitude');
$("#longitude").focus();
return false;
}

if($("#fluctuation_min").val()==""){
alert('Enter Water Table Fluctuations (m) Min');
$("#fluctuation_min").focus();
return false;
}
if($("#fluctuation_max").val()==""){
alert('Enter Water Table Fluctuations (m) Max');
$("#fluctuation_max").focus();
return false;
}
*/
$("#loading").slideDown("fast").html("Saving...");
jQuery.each(data,function(i,field){
a += " \"" + field.name + "\":\"" + field.value+"\"";
	//alert(a);
	
if(i+1 < data.length) a += ",";
//alert(data.length);
if(i==0) hp_id = field.value;						  
			});
a += "}";	



$.getJSON("call_ajax_sp_detail.php?new_handpump=" + a,function(data){
					$("#loading").css({"width":"300px","left":"400px"});
					//$(this).slideDown();					   
			
			// alert(data);
			 window.opener.location.reload();
			  
			 if(hp_id!=""){
				 
			  $("#hp_row_"+data.hp_id).html("<td>"+data.habitation_name+"</td><td>"+data.habit_type+"</td><td>"+data.location+"</td><td>"+data.latitude+"</td><td>"+data.longitude+"</td><td>"+data.function_status+"</td><td>"+data.opration_status+"</td><td>"+data.quality_status+"</td><td>"+data.yield+"</td><td>"+data.seasonal_flunctuation+"</td><td>"+data.fluctuation_min+"</td><td>"+data.fluctuation_max+"</td><td>"+ data.ph +"</td><td>"+ data.tds +"</td><td>"+ data.cl +"</td><td>"+ data.f +"</td><td>"+ data.no3 +"</td><td>"+ data.Repairable +"</td><td>"+ data.remarks+ "</td><td align='center'><a href='Javascript:void(0);' style='text-decoration:none; color:#000000;' onclick= \" hp_edit('"+ data.hp_id +"'); \" >&nbsp;<b>Edit</b></a>&nbsp;&nbsp;<a href='Javascript:void(0);' style='text-decoration:none; color:#000000;' onclick= \" hp_delete('"+ data.hp_id +"'); \" >&nbsp;<b>Delete</b></a></td>");
			 $("#loading").html("Your data has been changed successfully");
			 }
			 else{
				 
			  $("#handpump_content").append("<tr id='hp_row_"+ data.hp_id +"' class='hp_row'><td>"+data.habitation_name+"</td><td>"+data.habit_type+"</td><td>"+data.location+"</td><td>"+data.latitude+"</td><td>"+data.longitude+"</td><td>"+data.function_status+"</td><td>"+data.opration_status+"</td><td>"+data.quality_status+"</td><td>"+data.yield+"</td><td>"+data.seasonal_flunctuation+"</td><td>"+data.fluctuation_min+"</td><td>"+data.fluctuation_max+"</td><td>"+ data.ph +"</td><td>"+ data.tds +"</td><td>"+ data.cl +"</td><td>"+ data.f +"</td><td>"+ data.no3 +"</td><td>"+ data.Repairable +"</td><td>"+ data.remarks+ "</td><td align='center'><a href='Javascript:void(0);' style='text-decoration:none; color:#000000;' onclick= \" hp_edit('"+ data.hp_id +"'); \" >&nbsp;<b>Edit</b></a>&nbsp;&nbsp;<a href='Javascript:void(0);' style='text-decoration:none; color:#000000;' onclick= \" hp_delete('"+ data.hp_id +"'); \" >&nbsp;<b>Delete</b></a></td></tr>");
			 $("#loading").html("Your data has been add successfully");
			 }
			
			 $("#loading").fadeOut(5000);
			 $("#edit_mode").slideUp(1000);
			 close_event();
 });
return false;
}

function hp_edit(hp_id){
	$("#edit_mode").slideUp("slow");
	$(".hp_row").fadeOut(500);
	$("#hp_row_"+hp_id).fadeIn("slow");
	$("#loading").slideDown("fast").html("Loading").css({"width":"100px","left":"500px"});
	$.getJSON("call_ajax_sp_detail.php?edit_hp="+hp_id,function(data){
	
	$("#hp_id").val(data.id);
	$("#habitation_name").val(data.habitation_name);
	$("#habit_type").val(data.type_of_habitation);
	$("#location").val(data.location);
	$("#latitude").val(data.latitude);
	$("#longitude").val(data.longitude);
	
	if(data.functional_status=="FUNCTIONAL"){
		$("#function_on").attr("checked","checked");
		$("#func").show();
		$("#non_func").hide();

	}
	else{
		$("#function_off").attr("checked","checked");
		$("#func").hide();
		$("#non_func").show();
	}
	
	if(data.operational_status=="In Use") $("#opration_status_in").attr("checked","checked");
	else $("#opration_status_out").attr("checked","checked");
	
	if(data.quality_status=="Potable") $("#quality_status_port").attr("checked","checked");
	else $("#quality_status_not").attr("checked","checked");
	
	if(data.yeild=="Good") $("#yield_good").attr("checked","checked");
	else if(data.yeild=="Satisfactory") $("#yield_satis").attr("checked","checked");
	else $("#yield_poor").attr("checked","checked");
	
	if(data.sea_fluct_yeild=="No Change") $("#seasonal_flunctuation_no").attr("checked","checked");
	else if(data.sea_fluct_yeild=="Slight Change") $("#seasonal_flunctuation_slight").attr("checked","checked");
	else $("#seasonal_flunctuation_drastic").attr("checked","checked");
	
	if(data.repairable_status =="Non Repairable") $("#Repairable2").attr("checked","checked");
	else if(data.repairable_status =="Abondoned") $("#Repairable3").attr("checked","checked");
	else $("#Repairable1").attr("checked","checked");
	
	
	$("#fluctuation_min").val(data.water_table_fluct_min);
	$("#fluctuation_max").val(data.water_table_fluct_max);
	
	$("#ph").val(data.ph);
	$("#tds").val(data.tds);
	$("#cl").val(data.cl);
	$("#f").val(data.f);
	$("#no3").val(data.no3);
	
	
	$("#firstinput").val(data.para_date);
	
	$("#remarks").val(data.remark);
	$("#submit_form").val("Save Changes");
	$("#edit_mode").slideDown("slow");
	$("#loading").slideUp();
	});
	
	
	
}

function close_event(){
	$("#edit_mode").slideUp("slow",function(){
	$(".hp_row").fadeIn("slow");											
	});
}
function show_NF(){

	if($("#function_off").attr("checked")){
	$("#non_func").show();
	$("#func").hide();
	}
	else {
		$("#non_func").hide();
	$("#func").show();
	}
}


function hp_delete(id){
	//alert('sfsfs');
var r=confirm("Are you sure to delete this HP Detail?");

	if(r==true){
		var loc_id = $("#loc_id").val();
		$("#loading").slideDown("fast").html("Deleting...").css({"width":"100px","left":"500px"});
		$.get("call_ajax_sp_detail.php?del_id="+id+"&&loc_id="+loc_id,function(data){
		$("#hp_row_" + data).remove();
		$("#loading").html("Data has been successfully Deleted").css({"width":"300px","left":"400px"}).fadeOut(5000);
		$(".hp_row").fadeIn('slow');
		window.opener.location.reload();
		});
	}
}